var _excluded = ['children', 'detail', 'detailType', 'DetailComponent', 'FieldComponent'],
  _excluded2 = ['formItemClassName', 'formItemStyle', 'rules'];
function _extends() {
  _extends = Object.assign
    ? Object.assign.bind()
    : function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];
          for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }
        return target;
      };
  return _extends.apply(this, arguments);
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
import { omit, pick } from '@szzj/utils';
import { Form } from 'antd';
import React, { forwardRef, useMemo } from 'react';
import FieldDetailMap from '../FieldDetail/register';
import FormItem from '../FormItem';
import { useFormAttrsContext } from '../_utils/contexts/FormAttrsContext';
import { DetailFormItemPropNames, FormItemPropNames } from './consts';
import { enhanceFieldProps, enhanceRules } from './utils';
/**
 * 字段容器，整合 FormItem 及实际的表单控件
 * @param param0
 * @param ref
 * @returns
 */
function Field(_ref, ref) {
  var children = _ref.children,
    detailProp = _ref.detail,
    _ref$detailType = _ref.detailType,
    detailTypeProp = _ref$detailType === void 0 ? 'text' : _ref$detailType,
    DetailComponentProp = _ref.DetailComponent,
    FieldComponent = _ref.FieldComponent,
    rest = _objectWithoutProperties(_ref, _excluded);
  var form = Form.useFormInstance();
  var _ref2 = useFormAttrsContext(),
    detail = _ref2.detail;
  var isDetail = useMemo(
    function () {
      return detailProp !== undefined ? detailProp : detail;
    },
    [detail, detailProp],
  );
  var formItemProps = useMemo(
    function () {
      return pick(rest, isDetail ? DetailFormItemPropNames : FormItemPropNames);
    },
    [rest, isDetail],
  );
  var fieldProps = useMemo(
    function () {
      var result = omit(rest, FormItemPropNames);
      if (isDetail && formItemProps.valuePropName)
        result.valuePropName = formItemProps.valuePropName;
      return result;
    },
    [rest, formItemProps, isDetail],
  );
  var formItemClassName = formItemProps.formItemClassName,
    formItemStyle = formItemProps.formItemStyle,
    rules = formItemProps.rules,
    formItemRest = _objectWithoutProperties(formItemProps, _excluded2);
  var DetailComponent = useMemo(
    function () {
      var _FieldDetailMap$detai;
      return DetailComponentProp
        ? DetailComponentProp
        : (_FieldDetailMap$detai = FieldDetailMap[detailTypeProp]) !== null &&
          _FieldDetailMap$detai !== void 0
        ? _FieldDetailMap$detai
        : FieldDetailMap.text;
    },
    [DetailComponentProp, detailTypeProp],
  );
  if (isDetail) {
    return /*#__PURE__*/ React.createElement(
      FormItem,
      _extends(
        {
          className: formItemClassName,
          style: formItemStyle,
        },
        formItemRest,
      ),
      /*#__PURE__*/ React.createElement(DetailComponent, fieldProps, children),
    );
  }
  return /*#__PURE__*/ React.createElement(
    FormItem,
    _extends(
      {
        className: formItemClassName,
        style: formItemStyle,
      },
      formItemRest,
      {
        rules: enhanceRules(form, rules),
      },
    ),
    /*#__PURE__*/ React.createElement(
      FieldComponent,
      _extends({}, enhanceFieldProps(fieldProps, rules, form), {
        ref: ref,
      }),
      children,
    ),
  );
}
export default /*#__PURE__*/ forwardRef(Field);
